package com.common.base;

import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.common.R;
import com.common.application.ActivityStackManager;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by wqf on 16/4/28.
 *
 */
public abstract class BaseActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener, IView {
    protected Toolbar mToolbar;
    protected TextView mToolbarTitle;

    private ProgressDialog mDialog;
    private Unbinder mUnbinder;
    private Toast mToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initEnv();
        setUpContentView();
        bindView();

        setUpView();
        setUpData();

        ActivityStackManager.getInstance().pushActivity(this);
    }

    public void initEnv() {
    }

    public void bindView() {
        mUnbinder = ButterKnife.bind(this);
    }

    /**
     * 初始化 content view
     */
    protected abstract void setUpContentView();

    /**
     * 初始化 view
     */
    public abstract void setUpView();

    /**
     * view中填写数据
     */
    public abstract void setUpData();


    /**
     * 默认带有返回图标
     *
     * @param layoutResID
     */
    @Override
    public void setContentView(int layoutResID) {
        setContentView(layoutResID, -1, -1, MODE_BACK);
    }

    /**
     * @param layoutResID
     * @param titleResId  标题（ResourceId）
     */
    public void setContentView(int layoutResID, int titleResId) {
        setContentView(layoutResID, titleResId, -1, MODE_BACK);
    }

    /**
     * @param layoutResID
     * @param titleStr    标题（字符串）
     */
    public void setContentView(int layoutResID, String titleStr) {
        setContentView(layoutResID, titleStr, -1, MODE_BACK);
    }

    public void setContentView(int layoutResID, int titleResId, int mode) {
        setContentView(layoutResID, titleResId, -1, mode);
    }

    public void setContentView(int layoutResID, int titleResId, int menuId, int mode) {
        super.setContentView(layoutResID);
        setUpToolbar(titleResId > 0 ? getString(titleResId) : "", menuId, mode);
    }

    public void setContentView(int layoutResID, String titleStr, int menuId, int mode) {
        super.setContentView(layoutResID);
        setUpToolbar(titleStr, menuId, mode);
    }

    protected void setUpToolbar(String titleStr, int menuId, int mode) {
        if (mode == MODE_NONE) {
            return;
        }
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        if (mToolbar == null) {
            return;
        }
        mToolbar.setTitle("");
        mToolbarTitle = (TextView) findViewById(R.id.toolbar_title);

        if (mode == MODE_BACK) {
            mToolbar.setNavigationIcon(R.drawable.ic_return);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onNavigationBtnClicked();
                }
            });
        }

        if (!TextUtils.isEmpty(titleStr)) {
            setUpTitle(titleStr);
        }
        if (menuId > 0) {
            setUpMenu(menuId);
        }

    }

    /**
     * @param menuId
     */
    protected void setUpMenu(int menuId) {
        if (mToolbar != null) {
            mToolbar.getMenu().clear();
            if (menuId > 0) {
                mToolbar.inflateMenu(menuId);
                mToolbar.setOnMenuItemClickListener(this);
            }
        }
    }

    protected void setUpTitle(String titleStr) {
        if (!titleStr.equals("") && mToolbarTitle != null) {
            mToolbarTitle.setText(titleStr);
        }
    }

    protected void onNavigationBtnClicked() {
        finish();
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return false;
    }


    @Override
    public boolean isActivityFinished() {
        if (Build.VERSION.SDK_INT >= 17) {
            return this == null || isFinishing() || isDestroyed();
        } else {
            return this == null || isFinishing();
        }
    }

    @Override
    public void showLoading() {
        if (mDialog == null) {
            mDialog = new ProgressDialog(this);
        }
        mDialog.show();
    }

    @Override
    public void dismissLoading() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
        ActivityStackManager.getInstance().popActivity(this);
        mUnbinder.unbind();
    }

    @Override
    public void showToast(String msg) {
        if (mToast != null) {
            mToast.cancel();
        }
        mToast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
        mToast.show();
    }
}
